from rest_framework.permissions import OperationHolderMixin


class OperandHolder(OperationHolderMixin):
    def __init__(self, operator_class, *permissions):
        self.operator_class = operator_class
        self.permissions = permissions

    def __call__(self, *args, **kwargs):
        permissions = [i() for i in self.permissions]
        return self.operator_class(*permissions)

    def has_permission(self, request, view):
        return self().has_permission(request, view)

    def has_object_permission(self, request, view, obj):
        return self().has_object_permission(request, view, obj)


class AND:
    def __init__(self, *args):
        self.args = args

    def has_permission(self, request, view):
        return all(
            i.has_permission(request, view) for i in self.args
        )

    def has_object_permission(self, request, view, obj):
        return all(
            i.has_object_permission(request, view, obj) for i in self.args
        )


class OR:
    def __init__(self, *args):
        self.args = args

    def has_permission(self, request, view):
        return any(
            i.has_permission(request, view) for i in self.args
        )

    def has_object_permission(self, request, view, obj):
        return any(
            i.has_object_permission(request, view, obj) for i in self.args
        )


class NOT:
    def __init__(self, op1):
        self.op1 = op1

    def has_permission(self, request, view):
        return not self.op1.has_permission(request, view)

    def has_object_permission(self, request, view, obj):
        return not self.op1.has_object_permission(request, view, obj)
